<template>
  <view class="father">
    <!-- 搜素区域 的吸顶效果 -->
    <view class="search-box">
      <my-search @click="gotoSearch()"></my-search>
    </view>
    <!-- 轮播图区域  -->
    <view>
      <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
        <swiper-item v-for="(item,i) in swiperList" :key="i">
          <navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id">
            <image :src="item.image_src"></image>
          </navigator>
        </swiper-item>
      </swiper>
    </view>
    <!-- 分类区域 -->
    <view class="navList">
      <view class="nav" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 楼层区域 -->
    <view class="floorList">
      <!-- 楼层 item 项 -->
      <view class="flooritem" v-for="(item,i) in floorList" :key="i">
        <!-- 楼层标题 -->
        <image class="floortitle" :src="item.floor_title.image_src"></image>
        <!-- 楼层图片区域 -->
        <view class="floor-img-box">
          <!-- 左边图片 -->
          <navigator class="leftbox" :url="item.product_list[0].url">
            <image class="leftimage" :src="item.product_list[0].image_src"
              :style="{width:item.product_list[0].image_width+'rpx'}" mode="widthFix"></image>
          </navigator>

          <!-- 最右边图片 -->
          <view class="rightbox">
            <navigator v-for="(item1,i1) in item.product_list" :key="i1" v-if="i1!==0" :url="item1.url">
              <image :src="item1.image_src" mode="widthFix" :style="{width: item1.image_width + 'rpx'}"></image>
            </navigator>
          </view>


        </view>
      </view>

    </view>

  </view>


</template>

<script>
  export default {
    data() {
      return {
        //  轮播图数据列表
        swiperList: [],
        //  分类导航数据列表
        navList: [],
        floorList: [],

      };
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()

    },
    methods: {
      //  获取轮播图
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata');
        if (res.meta.status !== 200) return uni.$showMsg()
        this.swiperList = res.message
        // console.log(this.swiperList);
        uni.$showMsg('数据请求成功！')
      },
      // 获取分类导航
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.navList = res.message
        // console.log(this.navList);
      },
      // 分类导航的点击事件处理函数
      navClickHandler(item) {
        if (item.name !== '分类') return 0
        uni.switchTab({
          url: '/pages/cate/cate'
        })
      },
      // 获取楼层数据 /api/public/v1/home/floordata
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) return uni.$showMsg()
        //  处理URL
        res.message.forEach(floor => {
          floor.product_list.forEach(item => {
            item.url = '/subpkg/goods_list/goods_list?' + item.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
        // console.log(this.floorList);
      },
      gotoSearch() {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }

    }
  }
</script>

<style lang="scss">
  swiper {
    height: 330rpx;

    .swiper-item,
    image {
      width: 100%;
      height: 100%;
    }
  }

  .navList {
    display: flex;
    justify-content: space-around;
    /* 均匀排列每个元素每个元素周围分配相同的空间 */
    margin: 30rpx 0;

    .nav,
    image {
      width: 128rpx;
      height: 140rpx;
    }
  }

  .floortitle {
    height: 60rpx;
    width: 100%;
    display: flex;
  }



  .floor-img-box {
    display: flex;
    padding: 10rpx;

    .leftbox {
      margin-right: 8rpx;
    }

    .rightbox {
      display: flex;
      flex-wrap: wrap; //  指定flex属性的元素超出父元素换行
      justify-content: space-around;
    }
  }

  .search-box {
    position: sticky; //  类似于固定定位，但不会脱离标准流
    top: 0;
    z-index: 999; //  给搜索组件设置最高的层级
  }
</style>